package com.zhiyou100.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou100.model.Detail;
import com.zhiyou100.service.DetailService;
import com.zhiyou100.vo.ResponseVo;

@RestController
public class DetailController {
	@Autowired
	private DetailService detailService;
	@GetMapping("/details/{projectId}")
	public ResponseVo<ArrayList<Detail>> findBy(@PathVariable int projectId){
		ArrayList<Detail> al = detailService.findBy(projectId);
		return new ResponseVo<ArrayList<Detail>>(200, "请求成功", al);
	}
	
	@GetMapping("/details/{id}")
	public ResponseVo<Detail> findAll(@PathVariable int id){
		detailService.findAll(id);
		return new ResponseVo<Detail>(200, "请求成功", null);
	}
	@PostMapping("/details")
	public ResponseVo<String> add(Detail detail,MultipartFile file,HttpServletRequest request){
		

		String web=request.getServletContext().getRealPath("/");
		String path=web+"/"+file.getOriginalFilename();
		
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		
		detail.setVideo(file.getOriginalFilename());
		
		detailService.add(detail);
		
		return new ResponseVo<String>(200, "添加成功", null);
	}
	@PostMapping("/details")
	public ResponseVo<String> update(Detail detail,MultipartFile file,HttpServletRequest request){
		

		String web=request.getServletContext().getRealPath("/");
		String path=web+"/"+file.getOriginalFilename();
		
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		
		detail.setVideo(file.getOriginalFilename());
		
		detailService.update(detail);
		
		return new ResponseVo<String>(200, "添加成功", null);
	}

}
